<template>
  <div>
    <div class="header">
      <el-radio-group v-model="WaiterData" size="small" />
      <div style="margin: 20px" />
      <div class="userFace">
        <el-avatar :size="100">
          <img :src="WaiterData.userFace">
        </el-avatar>
        <el-button
          type="text"
          style="margin-right=50px"
          @click="editFace()"
        >修改个人信息</el-button>
      </div>
      <hr>
      <el-form label-width="80px">
        <el-form-item label="登录用户">{{ WaiterData.username }} </el-form-item>
        <el-form-item label="用户姓名">{{ WaiterData.realname }} </el-form-item>
        <el-form-item label="用户手机">{{ WaiterData.telephone }}</el-form-item>
        <el-form-item label="用户性别">{{ WaiterData.gender }} </el-form-item>
        <el-form-item label="用户角色">{{ roles }} </el-form-item>
        <el-form-item
          label="用户生日"
        >{{ WaiterData.birth | fmtDate }}
        </el-form-item>
        <el-form-item
          label="注册时间"
        >{{ WaiterData.registerTime | fmtDate }}
        </el-form-item>
        <el-form-item label="邮箱">{{ WaiterData.email }} </el-form-item>
        <el-form-item
          label="用户状态"
        ><el-tag type="success">
          {{ WaiterData.status }}
        </el-tag></el-form-item>
      </el-form>
    </div>
    <hr>
    <el-button type="text" @click="toEdit()">修改个人信息</el-button>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toClose">
      <el-form v-if="this.title == '修改头像信息'" ref="myForm" :model="form" ><el-upload
        :action="uploadFileURL"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img
          v-if="imageUrl"
          :src="form.userFace"
          class="avatar"
        >
        <i v-else class="el-icon-plus avatar-uploader-icon" />
      </el-upload>
      </el-form>

      <el-form v-else ref="myForm" :model="form" :rules="rules">
        <el-form-item
          label="登录用户"
          :label-width="labelWidth"
          prop="username"
        >
          <el-input
            v-model="form.username"
            disabled
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="姓名" :label-width="labelWidth" prop="realname">
          <el-input v-model="form.realname" autocomplete="off" />
        </el-form-item>
        <el-form-item label="性别" :label-width="labelWidth" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio label="男" />
            <el-radio label="女" />
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="用户手机"
          :label-width="labelWidth"
          prop="telephone"
        >
          <el-input v-model="form.telephone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="用户邮箱" :label-width="labelWidth" prop="email">
          <el-input v-model="form.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="出生日期" :label-width="labelWidth">
          <el-date-picker
            v-model="form.birth"
            type="datetime"
            placeholder="选择日期时间"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toClose">取 消</el-button>
        <el-button type="primary" @click="toSave">确定修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { waiterDetails, updateWaiter } from '@/api/waiter'
import { uploadFileURL } from "@/utils/config";

export default {
  data() {
    return {
      imageUrl:'',
      id: '',
      title: '',
      labelWidth: '120px',
      WaiterData: [],
      params: {},
      visible: false,
      form: {},
      rules: {
        username: [{ required: true, trigger: 'blur' }],
        gender: [{ required: true, trigger: 'blur' }]
      },
      roles: '员工',
      uploadFileURL,
    }
  },
  computed: {},
  created() {
    this.id = this.$route.query.id
    this.getWaiter()
  },
  mounted() {},
  methods: {
    async getWaiter() {
      const res = await waiterDetails({ id: this.id })
      this.WaiterData = res.data
    },
    toEdit() {
      this.title = '修改个人信息'
      this.visible = true
      this.form = { ...this.WaiterData }
    },
    editFace() {
      this.title = '修改头像信息'
      this.visible = true
      this.form = { ...this.WaiterData }
    },
    // 修改个人信息
    async toSave() {
      this.$refs.myForm.validate(async(valid) => {
        if (valid) {
          const res = await updateWaiter(this.form)
          this.$notify({
            type: 'success',
            message: res.message
          })
          this.getWaiter()
          this.toClose()
        } else {
          return false
        }
      })
    },
    toClose() {
      this.$refs.myForm.resetFields()
      this.visible = false
    },
    // 上传头像
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.userFace = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
